<?php

namespace App\Http\Controllers\Site\Custom\Provider;

use App\B2BProject\Custom\Provider\Offer;
use App\B2BProject\Site\HelperControllerBuildTrait;
use App\B2BProject\Custom\Provider\CounterOffer;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\B2BProject\Area;

class CounterOfferController extends Controller
{
    use HelperControllerBuildTrait;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

            $counter_offers = CounterOffer::whereHas('offer', function (Builder $builder) {
                $builder->where('member_id', \Auth::id());
            });

            if ($start = $request->query('time1')) {
                $counter_offers->where('created_at', '>=', date('Y-m-d', strtotime($start)));
            }

            if ($end = $request->query('time2')) {
                $counter_offers->where('created_at', '<=', date('Y-m-d', strtotime($end)));
            }

            if ($status = $request->query('state')) {
                $counter_offers->where('status', abs($status - 2) ^ 1);
            }

            $counter_offers = $counter_offers->orderBy('created_at', 'desc')->paginate($request->query('pageSize'));

            $data = [];

            foreach ($counter_offers as $counter_offer) {
                $data[] = [
                    'id'               => $counter_offer->id,
                    'goodsname'        => $counter_offer->offer->title,
                    'price'            => $counter_offer->price,
                    'amount'           => $counter_offer->amount,
                    'state'            => $counter_offer->status,
                    'counterofferdate' => date("Y-m-d H:i:s", strtotime($counter_offer->created_at)),
                    'district'         => Area::where('id',
                            $counter_offer->province_id)->first()->name . ' ' . Area::whereId($counter_offer->city_id)->first()->name,
                    'personname'       => $counter_offer->contact_name,
                    'tel'              => $counter_offer->contact_phone,
                ];
            }

            $responseText['data']           = $data;
            $responseText['pagingConfData'] = [
                'page'    => $counter_offers->currentPage(),
                'perpage' => $request->query('pageSize'),
                'total'   => $counter_offers->total()
            ];

            return response()->json($responseText);
        }

        return view('host/custom/provider/counter-offer/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * @param Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request)
    {
        if ($request->ajax()) {
            $id =$request->id;
            CounterOffer::findOrFail($id)->update(['status' => 0]);
            return $this->index($request);
        }
    }

    /**
     * @param Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function cancel(Request $request)
    {
        if ($request->ajax()) {
            $id =$request->id;
            CounterOffer::findOrFail($id)->delete();
            return $this->index($request);
        }
    }
}
